﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using Daidalos.Management;

namespace Daidalos.Exception
{
    public class GenericException : System.Exception
    {

        #region Attributes
        /// <summary>
        ///     Stores the description on the cause or the circumstances
        ///     of the exception.
        /// </summary>
        protected string errorDescription = null;
        #endregion

        #region Constructors
        /// <summary>
        ///     Hidden constructor.
        /// </summary>
        protected GenericException()
        {
        }

        /// <summary>
        ///     Create an exception using a describing text.
        /// </summary>
        /// <param name="errorText">
        ///     A string describing the reason and/or circumstances
        ///     that caused the exception.
        /// </param>
        public GenericException(string errorDescription)
        {
            // Store the error description.
            this.errorDescription = errorDescription;

            // Log the exception.
            Display();
            Log();
        }
        #endregion

        #region Methods
        /// <summary>
        ///     Displays the exception using a windows message box.
        /// </summary>
        public virtual void Display()
        {
            // Use a message box to display the exception.
            System.Windows.Forms.MessageBox.Show(errorDescription + "\n\n" + base.ToString(), "Generic exception caught", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
        }

        /// <summary>
        ///     Log the occurence of the exception using the log
        ///     manager.
        /// </summary>
        public virtual void Log()
        {
            // Log the exception.
            LogManager.Instance.Log("{generic exception} " + ToString());
        }

        /// <summary>
        ///     Create a string describing the exception. Use the information
        ///     supplied by the base class in addition to the error description.
        /// </summary>
        /// <returns>
        ///     A string describing the cause of the exception.
        /// </returns>
        override public string ToString()
        {
            return errorDescription;
        }
        #endregion

    }
}
